package com.lyq.leetcode.最长连续序列128;

import java.util.Random;
import java.util.TreeSet;

/**
 * @author lyq
 */
public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        int[] nums = new int[100];
        for (int i = 0; i < nums.length; i++) {
            nums[i]=new Random().nextInt();
        }
         System.out.printf("%d", main.longestConsecutive(new int[]{0,3,7,2,5,8,4,6,0,1}));
    }

    /**
     * 最长连续序列
     *利用map等数据结构进行匹配
     *
     * @param nums 输入数组
     * @return 最长连续序列个数
     */
    public int longestConsecutive(int[] nums) {
        if (nums.length==0){
            return 0;
        }
        TreeSet<Integer> treeSet = new TreeSet<>();
        for (int i:nums){
            treeSet.add(i);
        }
        int res=1;
        int tempCount= 1;
        int current = treeSet.first();
        System.out.println(treeSet);
        for (int item:treeSet){
            if (item != current){
                if (item-current==1){
                    tempCount++;
                }else {
                    res=Math.max(res,tempCount);
                    tempCount= 1;
                }
            }
            current=item;
        }
        return Math.max(res,tempCount);
    }
}
